/*
 * AddPlaceCommand.java
 *
 * Created on 26 maj 2007, 21:50
 */

package edu.uj.petri.commands;

import edu.uj.petri.document.Document;
import edu.uj.petri.model.Model;
import edu.uj.petri.model.Place;
import edu.uj.petri.view.PlaceView;
import edu.uj.petri.view.View;
import java.awt.geom.Point2D;
import java.util.ResourceBundle;

/**
 *
 * @author Paweł
 */
public class AddPlaceCommand extends Command {
    
    private PlaceView placeView;
    private Place place;
    private Document document;
    private Point2D position;
    /** if command is being executed for the first time; false on redo */
    private boolean firstTime=true;
    
    /** Creates a new instance of AddPlaceCommand */
    public AddPlaceCommand(Document document, Point2D position ) {
        
        this.document = document;
        this.position = position;
    }

    public String toString() {
        String retval = ResourceBundle.getBundle("edu/uj/petri/messages").getString("dodanie_miejsca");
        if (place != null) {
            retval += " " + place.getName();
        }
        return retval;
    }

    void execute() {
        Model model = document.getModel();
        View view = document.getView();
        
        if (firstTime) {
            place = model.createPlace();
            placeView = new PlaceView( place, position );
            firstTime=false;
        }
        
        model.addPlace(place);  // TODO OK? - by Piotrek
        view.addElement( placeView );
        view.draw();
    }

    void undo() {
        Model model = Document.getActiveDocument().getModel();
        View view = document.getView();
        
        model.deletePlace(place);
        view.removeElement( placeView );
        view.draw();
    }

    boolean canBeCombined(Command c) {
        
        return false;
    }

    private static final long serialVersionUID = 5757019990555500400L;
    
}
